package com.ewing.utils;

import java.util.UUID;

/**
 * 业务流水工具类
 * 
 * @author tanson lam
 * @createDate 2015年2月12日
 * 
 */
public class BizGenerator {

	/**
	 * 生成业务流水号
	 * 
	 * @return
	 */
	public static String generateBizNum() {
		String a = (String.valueOf(System.currentTimeMillis()));
		String d = (String.valueOf(Math.random())).substring(2, 10);
		return "EWING-" + a + d;
	}

	public static String generateUUID() {
		String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
		return uuid;
	}
	
	/**
	 * 创建指定数量的随机字符串
	 * 
	 * @param numberFlag
	 *            是否是数字
	 * @param length
	 * @return
	 */
	public static String generateRandomCode(boolean numberFlag, int length) {
		String retStr = "";
		String strTable = numberFlag ? "1234567890"
				: "1234567890abcdefghijkmnpqrstuvwxyz";
		int len = strTable.length();
		boolean bDone = true;
		do {
			retStr = "";
			int count = 0;
			for (int i = 0; i < length; i++) {
				double dblR = Math.random() * len;
				int intR = (int) Math.floor(dblR);
				char c = strTable.charAt(intR);
				if (('0' <= c) && (c <= '9')) {
					count++;
				}
				retStr += strTable.charAt(intR);
			}
			if (count >= 2) {
				bDone = false;
			}
		} while (bDone);

		return retStr;
	}

}
